//
//  ItemView.swift
//  Persistance
//
//  Created by apple on 2018/10/26.
//  Copyright © 2018 apple. All rights reserved.
//

import UIKit
import CTPersistance

@objcMembers class ItemView: UIView {

    var name: String? {
        
        didSet {
            
            self.nameLabel.text = name
        }
    }
    var detail: String? {
        
        didSet {
            self.detailLabel.text = detail
        }
    }
    var thisPropertyWillNotBeSaved: String = "this property will not be saved if you send this view to data center for inserting"
    
    
    init() {
        super.init(frame: CGRect.zero)
        
        addSubview(nameLabel)
        addSubview(detailLabel)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        nameLabel.sizeToFit()
        nameLabel.center = CGPoint(x: self.bounds.size.width/2.0, y: self.bounds.size.height/2.0)
        
        detailLabel.sizeToFit()
        detailLabel.center = CGPoint(x: self.bounds.size.width/2.0, y: self.bounds.size.height/2.0)
        detailLabel.frame = CGRect(x: detailLabel.frame.origin.x, y: nameLabel.frame.origin.y + 5 + nameLabel.frame.height, width: detailLabel.frame.width, height: detailLabel.frame.height)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var nameLabel: UILabel = {
        
        let nameLabel = UILabel()
        nameLabel.font = UIFont.systemFont(ofSize: 20.0)
        nameLabel.textColor = UIColor.red
        return nameLabel
    }()
    
    private lazy var detailLabel: UILabel = {
        
        let detailLabel = UILabel()
        detailLabel.font = UIFont.systemFont(ofSize: 15.0)
        detailLabel.textColor = UIColor.black
        return detailLabel
    }()
    
}

var kitemRecordPrimaryKey = 100
var kitemDetailRecordPrimaryKey = 100
var kprimaryKey = 100


extension ItemView: CTPersistanceRecordProtocol {
    
    
    func dictionaryRepresentation(with table: (CTPersistanceTable & CTPersistanceTableProtocol)!) -> [AnyHashable : Any]! {
        
        var result = [AnyHashable : Any]()
        if table is ItemTable {
             result["name"] = self.name
        }
        
        if table is ItemDetailTable {
             result["detail"] = self.detail
        }
        
        return result
    }
    
    func objectRepresentation(with dictionary: [AnyHashable : Any]!) {
        
        self.name = dictionary["name"] as? String
        self.detail = dictionary["detail"] as? String
    }
    
    func mergeRecord(_ record: CTPersistanceRecordProtocol!, shouldOverride: Bool) -> CTPersistanceRecordProtocol! {
        
        let availableKeyList = self.availableKeyList()
        
        let record1 = record as! NSObject&CTPersistanceRecordProtocol
        for item in availableKeyList ?? [] {
            
            if item is String {
                
                let key = item as! String
                
                if record1.getPropertyNames().contains(key) {
                    
                    let recordValue = record1.value(forKey: key)
                    let selfValue = self.value(forKey: key);
                    
                    if (shouldOverride || selfValue == nil) {
                        self.setValue(recordValue, forKey: key)
                    }
                }
               
                
            }
        }
        return self
    }
    
    func availableKeyList() -> [Any]! {
        
        return ["name", "detail"]
    }
    //MARK: getter and setter
    var itemRecordPrimaryKey: NSNumber? {
        
        get{
            return objc_getAssociatedObject(self, &kitemRecordPrimaryKey) as? NSNumber
        }
        
        set {
            
            objc_setAssociatedObject(self, &kitemRecordPrimaryKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var itemDetailRecordPrimaryKey: NSNumber? {
        
        get{
            return objc_getAssociatedObject(self, &kitemDetailRecordPrimaryKey) as? NSNumber
        }
        
        set {
            
            objc_setAssociatedObject(self, &kitemDetailRecordPrimaryKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var primaryKey: NSNumber? {
        
        get{
            return objc_getAssociatedObject(self, &kprimaryKey) as? NSNumber
        }
        
        set {
            
            objc_setAssociatedObject(self, &kprimaryKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
}
